<?php
namespace common\models\goods;

use Yii;
use yii\db\ActiveRecord;
//use yii\behaviors\TimestampBehavior;
use common\enums\StatusEnum;
use common\helpers\RegularHelper;

class GoodsAttr extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%goods_attr_index}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['goods_id', 'goods_commonid','attr_id','attr_value_id'], 'required'],
            [['goods_id', 'goods_commonid','attr_id','attr_value_id'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'goods_id' => '商品ID',
            'goods_commonid' => '商品公共ID',
            'attr_id' => '属性ID',
            'attr_value_id' => '属性值ID',
        ];
    }

    /**
     * 获取属性
     * @return \yii\db\ActiveQuery
     */
    public function getAttr(){
        return $this->hasOne(Attr::className(),['attr_id'=>'attr_id'])
            ->select(['attr_id','attr_name']);
    }

    /**
     * 获取属性值
     * @return \yii\db\ActiveQuery
     */
    public function getAttr_value(){
        return $this->hasOne(AttrValue::className(),['attr_value_id'=>'attr_value_id'])
            ->select(['attr_value_id','attr_value_name']);
    }

    /**
     * 场景
     *
     * @return array
     */
    public function scenarios()
    {
        return [
//            'create' => [''],
//            'backendCreate' => ['username', 'password_hash'],
            'default' => array_keys($this->attributeLabels()),
        ];
    }

    /**
     * @return array
     */
//    public function behaviors()
//    {
//        return [
//            [
//                'class' => TimestampBehavior::class,
//                'attributes' => [
//                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
//                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
//                ],
//            ],
//        ];
//    }
}
